/*
 * ============================================================================
 *                   GNU Lesser General Public License
 * ============================================================================
 *
 *
 *
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 * 
 *
 *
 */
package rad.framework.el;

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;

import org.apache.commons.lang.StringUtils;

/**
 * Provide utilities for using expression language outside of JSF.
 * 
 * @author boudyacho
 */
public class ExpressionUtil {

	static {
		if (StringUtils.isEmpty(getFactoryClass())) {
			setFactoryClass("org.jboss.el.ExpressionFactoryImpl");
		}
	}

	public static void setFactoryClass(String factoryClass) {
		System.setProperty("javax.el.ExpressionFactory", factoryClass);
	}

	public static String getFactoryClass() {
		return System.getProperty("javax.el.ExpressionFactory");
	}

	public static ExpressionFactory getFactory() {
		return ExpressionFactory.newInstance();
	}

	public static ValueExpression createValueExpression(ELContext context,
			String expression, Class type) {
		return getFactory().createValueExpression(context, expression, type);
	}

	public static <T> T getValue(ELContext context, String expression,
			Class<T> type) {
		ValueExpression exp = createValueExpression(context, expression, type);
		return (T) exp.getValue(context);
	}

	public static Object getValue(ELContext context, String expression) {
		return getValue(context, expression, Object.class);
	}
}
